package com.asset.modules.system.controller;

import com.asset.modules.common.model.ParamBean;
import com.asset.modules.common.model.ResponseBean;
import com.asset.modules.common.utils.Constant;
import com.asset.modules.common.utils.StringUtil;
import com.asset.modules.system.model.Company;
import com.asset.modules.system.service.ProjectService;
import com.asset.modules.system.service.UserService;
import com.asset.modules.system.service.impl.CompanyServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenchen
 */
@RestController
@CrossOrigin
@RequestMapping("/company")
@Api(tags = "区域信息接口")
public class CompanyController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CompanyController.class);

    private static final String PROJECT_FAILED_MSG="待删除区域有关联项目公司数据，请先删除关联公司数据!";

    private static final String USER_FAILED_MSG="待删除区域有关联项目公司数据，请先删除关联公司数据!";


    @Autowired
    CompanyServiceImpl companyService;

    @Autowired
    ProjectService ps;

    @Autowired
    UserService us;

    @ApiOperation(value = "区域信息的保存和更新",notes = "参数中带id是更新，不带是新建")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean saveOrUpdate(@RequestBody Company company){
        try{
            companyService.saveOrUpdate(company);
            return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, true);
        }catch (Exception e){
            LOGGER.error(e.getMessage());
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "信息保存失败，请查看编码和名称是否重复!", false);
        }

    }

    @ApiOperation(value = "根据ID获取区域信息")
    @RequestMapping(value = "/byId", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean getCompanyById(@RequestParam String id){
        Company company = companyService.getById(id);
        return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, company);
    }


    @ApiOperation(value = "根据code获取区域信息")
    @RequestMapping(value = "/byCode", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean getCompanyByCode(@RequestParam String code){
        return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, companyService.getCompanyByCode(code));
    }

    @ApiOperation(value = "根据name获取区域名称")
    @RequestMapping(value = "/byName", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean getCompanyByName(@RequestParam String name){
        return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, companyService.getCompanyByName(name));
    }

    @ApiOperation(value = "获取所有区域信息")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean getCompanies(){
        List<Company> list = companyService.list();
        if(list==null||list.size()==0){
            return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, Constant.NO_RESULT);
        }
        return new ResponseBean(HttpStatus.OK.value(), Constant.SUCCESS_REQUEST, list);
    }

    @ApiOperation(value = "删除区域信息")
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean removeCompany(@RequestParam String ids){
        String msg = "删除区域信息";
        if(StringUtil.isBlank(ids)){
            msg  = "待删除区域ID为空";
            return new ResponseBean(HttpStatus.OK.value(), msg, false);
        }
        String[] removeIds = ids.split(",");
        List list = new ArrayList();
        for(String id : removeIds){
            boolean projectExistFlag = ps.isExist("company_id", id);
            boolean userExistFlag = us.isExist("company_id", id);
            if(projectExistFlag){
                return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), PROJECT_FAILED_MSG, id);
            }

            if(userExistFlag){
                return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), USER_FAILED_MSG, id);
            }

            list.add(id);
        }

        boolean flag = companyService.removeByIds(list);
        return new ResponseBean(HttpStatus.OK.value(), msg, flag);
    }


}
